package com.qf.mybatisplus.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送短信API
 */
public class SendMessageUtil {
    //账号：YZM1026051密码：ZwjKQlAW1Ra4b5(验证码短信)
    private static final String account = "N7512464";

    private static final String password = "8AbDRBHWQG512f";

    public static String sendMessage(String content,String params) {

        String sendUrl = "https://smssh1.253.com/msg/variable/json";
        Map map = new HashMap();
        //API账号
        map.put("account", account);
        //API密码
        map.put("password", password);
        //短信内容
        map.put("msg", content);
        //手机号,参数
        map.put("params", params);
        //是否需要状态报告
        map.put("report", "true");
        //自定义扩展码
        map.put("extend", "123");
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        String response = sendSmsByPost(sendUrl, js.toString());
        return response;

    }


    public static String sendNotice(String content, String phone) {
        // https://xxx/msg/send/json
        String sendUrl = "https://smssh1.253.com/msg/send/json";
        Map map = new HashMap();
        //API账号
        map.put("account", account);
        //API密码
        map.put("password", password);
        //短信内容
        map.put("msg", content);
        //手机号
        map.put("phone", phone);
        //是否需要状态报告
        map.put("report", "true");
        //自定义扩展码
        map.put("extend", "123");
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        String response = sendSmsByPost(sendUrl, js.toString());
        return response;
    }


    public static String sendSmsByPost(String path, String postContent) {

        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
